package com.flca.frw.util
{
	import flash.display.DisplayObject;
	import mx.controls.RadioButtonGroup;
	
	public class ValidationUtil 
	{
		import mx.validators.Validator;
		import mx.events.ValidationResultEvent;
			
		public static var skipValidation : Boolean = false;
		
		public static function enableValidators(aValidators:Array, aEnabledFlag:Boolean):void
		{
			var vld:Validator;
				
			for (var i:int = 0 ; i < aValidators.length; i++)
			{
				vld = aValidators[i] as Validator;
				vld.enabled = aEnabledFlag;
			}
		}
		
		public static function validateAll(aValidators:Array):Boolean
		{
			var validator:Validator;
			var tmpResult:ValidationResultEvent;
			var result:Boolean;
			
			result = true;
			if (!skipValidation) {
				for (var i:String in aValidators) 
				{
					validator = Validator(aValidators[i]);
					var srcobj:Object = validator.source;
					if (isObjectVisible(srcobj)) {
						tmpResult = validator.validate();
						if (tmpResult != null && tmpResult.type==ValidationResultEvent.INVALID)
						{
							result = false;
						}
					}
				}
			}
			
			return result;
		}
		
		public static function isObjectVisible(srcobj:Object) : Boolean {
			
			if (srcobj is DisplayObject) {
				var displayObj:DisplayObject = srcobj as DisplayObject;
				if (!displayObj.visible) {
					return false;
				}

				var parentobj:Object = srcobj.parent;
				while (parentobj != null) {
					if (parentobj is DisplayObject) {
						if (!parentobj.visible) {
							return false;
						}
						parentobj = parentobj.parent;
					} 
				}
			}
			
			return true;
		}
	}
}